<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


<script type="text/javascript">
	$(function() {
		parent.$.messager.progress({
			text : '数据加载中....'
		});
		$('#usertree').tree({
			url : '${ctx}/sys/role/userlist/tree',
			checkbox : true,
			idFiled:'id',
			textFiled:'text',
			parentField :'pid',
			onLoadSuccess : function(node,data) {
				
					  
					  $.ajax({
						  url: '${ctx}/sys/role/userlist/treebyid',
						  async:false,
						  data:{userid:"${userid}"},
						  success: function(data){
							 for (var i = 0; i < data.length; i++) {
									var node = $('#usertree').tree('find', data[i].id);
									if (node) {
										var isLeaf = $('#usertree').tree('isLeaf', node.target);
										if (isLeaf) {
											$('#usertree').tree('check', node.target);
										}
									}
								}
							  parent.$.messager.progress('close');
							  collapseAll();
						  }
					});
				
			}
		});
		
		$("#userfunctionListPanel").panel({
					title :"菜单列表",
					tools:[{iconCls:'icon-save',handler:function(){usermysubmit();}}]
		});
	});
	
	function usermysubmit() {
		var nodes = $('#usertree').tree('getChecked');
		var ids = [];
		for (var i = 0; i < nodes.length; i++) {
			ids.push(nodes[i].id);
		}
		parent.$.messager.progress({
			text : '正在提交数据....'
		});
		$.post('${ctx}/sys/userRole/userjiao/quan', {
			id : $('#userid').val(),
			ids : ids.join(',')
		}, function(result) {
			parent.$.messager.progress('close');
			if(result.success){
				reset();
				$.messager.alert('提示', "授权成功");
			}else{
				$.messager.alert('提示', "授权失败");
			}
		}, 'json');
	}
	function reset() {
		$('#usertree').tree('reload');
	}


 
</script>
<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'center'" style="padding: 1px;">
		<div  class="easyui-panel" title="角色列表" style="padding: 1px;"
			data-options="fit:true,border:false" id="userfunctionListPanel">
			<input type="hidden" name="id" value="${userid}" id="userid">
			
			<ul id="usertree"></ul>
		</div>
	</div>
</div>
